<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.pethospital.service.impl.PetServiceImpl" %>
<%@ page import="com.pethospital.service.PetService" %>
<%@ page import="com.pethospital.entity.Pet" %>
<%@ page import="com.pethospital.entity.User" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html>
<head>
    <title>直接测试页面</title>
    <meta charset="UTF-8">
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .debug { background-color: #f0f0f0; padding: 10px; margin: 10px 0; border: 1px solid #ccc; }
        .error { color: red; }
        .success { color: green; }
    </style>
</head>
<body>
    <h1>直接测试页面</h1>
    
    <%
        // 获取当前用户
        User currentUser = (User) session.getAttribute("currentUser");
        
        out.println("<div class='debug'>");
        out.println("<h2>当前用户信息:</h2>");
        if (currentUser != null) {
            out.println("用户ID: " + currentUser.getUserId() + "<br>");
            out.println("用户名: " + currentUser.getUsername() + "<br>");
            out.println("真实姓名: " + currentUser.getRealName() + "<br>");
            out.println("用户类型: " + currentUser.getUserType() + "<br>");
        } else {
            out.println("<span class='error'>当前用户为空，请先登录</span><br>");
        }
        out.println("</div>");
        
        if (currentUser != null) {
            // 测试宠物服务
            PetService petService = new PetServiceImpl();
            
            out.println("<div class='debug'>");
            out.println("<h2>直接调用PetService测试:</h2>");
            List<Pet> myPets = petService.getPetsByOwnerId(currentUser.getUserId());
            
            if (myPets != null && !myPets.isEmpty()) {
                out.println("<span class='success'>找到 " + myPets.size() + " 只宠物:</span><br>");
                for (Pet pet : myPets) {
                    out.println("- ID: " + pet.getPetId() + 
                               ", 名称: '" + pet.getPetName() + "'" + 
                               ", 类型: '" + pet.getPetType() + "'" + 
                               ", 品种: '" + pet.getBreed() + "'" + 
                               ", 主人ID: " + pet.getOwnerId() + "<br>");
                }
                
                // 设置到request中供JSTL使用
                request.setAttribute("myPets", myPets);
                
            } else {
                out.println("<span class='error'>没有找到宠物数据</span><br>");
                
                // 创建测试宠物
                myPets = new ArrayList<>();
                Pet testPet = new Pet();
                testPet.setPetId(999);
                testPet.setPetName("测试宠物");
                testPet.setPetType("狗");
                testPet.setBreed("测试品种");
                testPet.setOwnerId(currentUser.getUserId());
                myPets.add(testPet);
                
                request.setAttribute("myPets", myPets);
                out.println("<span class='success'>已创建测试宠物</span><br>");
            }
            out.println("</div>");
            
            out.println("<div class='debug'>");
            out.println("<h2>JSTL测试:</h2>");
        }
    %>
    
    <c:if test="${not empty myPets}">
        <div class="success">JSTL能够访问宠物数据，数量: ${myPets.size()}</div>
        <ul>
            <c:forEach var="pet" items="${myPets}">
                <li>ID: ${pet.petId}, 名称: "${pet.petName}", 类型: ${pet.petType}</li>
            </c:forEach>
        </ul>
        
        <h3>选择框测试:</h3>
        <select id="testSelect" style="width: 300px; padding: 5px;">
            <option value="">请选择宠物</option>
            <c:forEach var="pet" items="${myPets}">
                <option value="${pet.petId}">${pet.petName} (${pet.petType})</option>
            </c:forEach>
        </select>
        
        <script>
            document.addEventListener('DOMContentLoaded', function() {
                const select = document.getElementById('testSelect');
                console.log('选择框选项数量:', select.options.length);
                for (let i = 0; i < select.options.length; i++) {
                    console.log('选项 ' + i + ':', select.options[i].value, select.options[i].text);
                }
            });
        </script>
        
    </c:if>
    
    <c:if test="${empty myPets}">
        <div class="error">JSTL无法访问宠物数据</div>
    </c:if>
    
    <%
        if (currentUser != null) {
            out.println("</div>");
        }
    %>
    
    <hr>
    <h2>测试链接:</h2>
    <p><a href="${pageContext.request.contextPath}/appointment/add">访问预约页面</a></p>
    <p><a href="${pageContext.request.contextPath}/debug-pets.jsp">访问调试页面</a></p>
    <p><a href="${pageContext.request.contextPath}/fix-pet-data.jsp">访问数据修复页面</a></p>
</body>
</html>
